import 'package:getx_tutorial/app/data/models/news_model.dart';
import 'package:getx_tutorial/app/data/providers/news_provider.dart';

abstract class INewsRepository {
  Future<NewsList> getNewsList({String type = 'top', int page = 1});

  Future<NewsItem> getNewsDetail(String uniquekey);
}

class NewsRepository implements INewsRepository {
  NewsRepository({required this.provider});
  final INewsProvider provider;

  @override
  Future<NewsList> getNewsList({String type = 'top', int page = 1}) async {
    final response = await provider.getNewsList(type: type, page: page);
    // print('=== Repository: ');
    // print(response);

    if (response.status.hasError) {
      return Future.error(response.statusText!);
    } else {
      return response.body!;
    }
  }

  @override
  Future<NewsItem> getNewsDetail(String uniquekey) async {
    print('res: '+uniquekey);
    final response = await provider.getNewsDetail(uniquekey);
    print('=== Repository - getNewsDetail: ');
    print(response.body);

    if (response.status.hasError) {
      return Future.error(response.statusText!);
    } else {
      return response.body!;
    }
  }
}